/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xuexiang.xuidemo.slice.imageview;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.utils.Color;

import com.xuexiang.xui_lib.component.dialog.config.ChooseBean;
import com.xuexiang.xui_lib.component.dialog.config.ConfigBean;
import com.xuexiang.xui_lib.component.dialog.interfaces.ItemDialogListener;
import com.xuexiang.xui_lib.component.dialog.view.StyledDialog;
import com.xuexiang.xui_lib.component.imageview.RadiusImageView;
import com.xuexiang.xuidemo.ResourceTable;

import java.util.ArrayList;

/**
 * 圆角图片
 *
 * @since 2021-04-08
 */
public class RadiusImageViewSlice extends AbilitySlice {
    RadiusImageView mCircleImage, mEllipseImage, mRectangleImage, mCircleBigImage, mRectangleBigImageOther;
    Image image_right;
    ConfigBean configBean = null;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_radius_image);
        initView();
    }

    private void initView() {
        Image image_back = (Image) findComponentById(ResourceTable.Id_image_back);
        image_back.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
        mCircleImage = (RadiusImageView) findComponentById(ResourceTable.Id_circle_image);
        mEllipseImage = (RadiusImageView) findComponentById(ResourceTable.Id_ellipse_image);
        mRectangleImage = (RadiusImageView) findComponentById(ResourceTable.Id_rectangle_image);
        mCircleBigImage = (RadiusImageView) findComponentById(ResourceTable.Id_circle_big_image);
        mCircleImage.setPixelMapToCircleImage(ResourceTable.Media_img_beautiful_scenery);
        mEllipseImage.setPixelMapToCircleImage(ResourceTable.Media_img_beautiful_scenery);
        mRectangleImage.setPixelMapToCircleImage(ResourceTable.Media_img_beautiful_scenery);
        mCircleBigImage.setPixelMapToCircleImage(ResourceTable.Media_img_beautiful_scenery);
        image_right = (Image) findComponentById(ResourceTable.Id_image_right);
        mRectangleBigImageOther = (RadiusImageView) findComponentById(ResourceTable.Id_rectangle_big_image_other);
        mRectangleBigImageOther.setPixelMapToCircleImage(ResourceTable.Media_img_beautiful_scenery);
        mRectangleBigImageOther.setIsSelected(false);
        image_right.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                CharSequence[] words = {"改变边框颜色与宽度", "改变选中态边框颜色与宽度", "改变选中态 Mask 的颜色（带透明度）", "手工切换选中态", "改变圆角大小", "变为圆形", "变为椭圆形", "禁止使用touchSelect态", "重置为初始态"};
                boolean[] selectItems = {true, false, false, false, false, false, false, false, false};

                ArrayList<ChooseBean> list = new ArrayList<>();
                for (int i = 0; i < words.length; i++) {
                    ChooseBean chooseBean = new ChooseBean();
                    chooseBean.setTxt(words[i]);
                    chooseBean.setChoosen(selectItems[i]);
                    list.add(chooseBean);
                }
                configBean = StyledDialog.buildMdIMAGEList(getContext(), list, new ItemDialogListener() {
                    @Override
                    public void onItemClick(CharSequence text, int position) {
                        reset();
                        mRectangleBigImageOther.setIsSelected(true);
                        mRectangleBigImageOther.setPosition(position);
                        configBean.commonDialog.hide();
                        switch (position) {
                            case 0:
                                mRectangleBigImageOther.setBorderWidth(12);
                                mRectangleBigImageOther.setBorderColor(Color.BLACK);
                                break;
                            case 1:
                                mRectangleBigImageOther.setTouchSelectModeEnabled(true);
                                break;
                            case 3:
                                mRectangleBigImageOther.setMaskColor(Color.getIntColor("#7FEF5362"));
                                break;
                            case 4:
                                mRectangleBigImageOther.setCornerRadius(60);
                                mRectangleBigImageOther.setBorderColor(new Color(Color.getIntColor("#FE6D4B")));
                                break;
                            case 5:
                                mRectangleBigImageOther.setVisibility(Component.HIDE);
                                mCircleBigImage.setVisibility(Component.VISIBLE);
                                mCircleBigImage.setBorderColor(new Color(Color.getIntColor("#FE6D4B")));
                                mCircleBigImage.setBorderWidth(10);
                                break;
                            case 6:
                                mRectangleBigImageOther.setOval(true);
                                break;
                            case 7:
                                mRectangleBigImageOther.setTouchSelectModeEnabled(false);
                                break;
                            default:
                                break;
                        }
                    }


                    @Override
                    public void positiveButton() {
                        if (configBean.commonDialog != null) {
                            configBean.commonDialog.hide();
                            configBean.commonDialog = null;
                        }

                    }

                    @Override
                    public void negativeButton() {
                        if (configBean.commonDialog != null) {
                            configBean.commonDialog.hide();
                            configBean.commonDialog = null;
                        }
                    }
                });
                configBean.commonDialog.show();
                configBean.property.start();
            }

            private void reset() {
                mCircleBigImage.setVisibility(Component.HIDE);
                mRectangleBigImageOther.setVisibility(Component.VISIBLE);
                mRectangleBigImageOther.setBorderWidth(6);
                mRectangleBigImageOther.setCornerRadius(40);
                mRectangleBigImageOther.setCircle(false);
                mRectangleBigImageOther.setOval(false);
                mRectangleBigImageOther.setBorderColor(new Color(Color.getIntColor("#FE6D4B")));
                mRectangleBigImageOther.setTouchSelectModeEnabled(true);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        mRectangleBigImageOther.setIsSelected(true);
        mRectangleBigImageOther.setBorderWidth(1);
        mRectangleBigImageOther.setBorderColor(Color.BLACK);
    }
}

